package com.yinhe.music.yhmusic.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.model.DataItem;
import com.yinhe.music.yhmusic.model.SingerList;
import com.yinhe.music.yhmusic.singer.parse.Singer;
import com.yinhe.music.yhmusic.singer.parse.SingerParseUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/8/15.
 */

public class SingerAdapter extends BaseAdapter {


    private List<DataItem>   dataItemList   = new ArrayList<>();

    public  List<SingerList> mHotSingerList = new ArrayList<>();
    public  List<SingerList> mFreSingerList = new ArrayList<>();
    private HotSingerAdapter mHotAdapter    = new HotSingerAdapter();
    private HotSingerAdapter mRecentAdapter = new HotSingerAdapter();

    public interface SingerAdapterListener {

        /**
         * 歌手点击事件
         *
         * @param singer 歌手信息
         */
        void singerDidClick(Singer singer);
    }

    private SingerAdapterListener mListener;
    private Context mContext;

    public SingerAdapter(Context context, SingerAdapterListener listener) {
        mListener = listener;
        mContext = context;
    }

    public void updateData(List<DataItem> items) {
        if (items != null) {
            dataItemList.clear();
            dataItemList.addAll(items);
            notifyDataSetChanged();
        }
    }

    public int getSelectIndex(String a) {
        for (DataItem item : dataItemList) {
            if (item.getType() != DataItem.TYPE_SINGER
                    && item.getCharacter().equals(a)) {
                return dataItemList.indexOf(item);
            }
        }
        return -1;
    }

    @Override
    public int getCount() {
        return dataItemList.size();
    }

    @Override
    public DataItem getItem(int i) {
        return dataItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        int type = getItemViewType(i);

        ViewHolder viewHolder = null;
        switch (type) {
            case DataItem.TYPE_SINGER:
                if (view == null) {
                    view = LayoutInflater.from(mContext).inflate(R.layout.singer_item, viewGroup, false);
                    viewHolder = new ViewHolder();
                    viewHolder.imageView = view.findViewById(R.id.image_view);
                    viewHolder.nameView = view.findViewById(R.id.name_view);
                    viewHolder.lineView = view.findViewById(R.id.line);
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) view.getTag();
                }
                break;
            case DataItem.TYPE_CHARACTER:
                if (view == null) {
                    view = LayoutInflater.from(mContext).inflate(R.layout.singer_character_item, viewGroup, false);
                    viewHolder = new ViewHolder();
                    viewHolder.nameView = view.findViewById(R.id.name_view);
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) view.getTag();
                }
                break;

            case DataItem.TYPE_HOT:
                if (view == null) {
                    view = LayoutInflater.from(mContext).inflate(R.layout.hot_singer_header, viewGroup, false);
                    viewHolder = new ViewHolder();
                    viewHolder.recyclerView = view.findViewById(R.id.hot_singer_view);
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) view.getTag();
                }
                break;
            case DataItem.TYPE_RECENTLY:
                if (view == null) {
                    view = LayoutInflater.from(mContext).inflate(R.layout.recently_singer_header, viewGroup, false);
                    viewHolder = new ViewHolder();
                    viewHolder.recyclerView = view.findViewById(R.id.recently_singer_view);
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) view.getTag();
                }
                break;
        }

        if (viewHolder == null) return null;
        final DataItem item = dataItemList.get(i);
        switch (item.getType()) {
            case DataItem.TYPE_CHARACTER:
                viewHolder.nameView.setText(item.getCharacter());
                view.setOnClickListener(null);
                break;
            case DataItem.TYPE_SINGER:
                viewHolder.nameView.setText(item.getSinger().name);
                viewHolder.imageView.setImageResource(R.drawable.ic_default_singer);
                viewHolder.lineView.setVisibility(item.isBottom() ? View.INVISIBLE : View.VISIBLE);
                view.setOnClickListener((v) -> {
                    if (mListener != null) {
                        mListener.singerDidClick(item.getSinger());
                    }
                });
                GlideHelper.setCircleImageResource(viewHolder.imageView, SingerParseUtil.getSingerImage(item.getSinger().image), R.drawable.ic_default_singer);
                break;
            case DataItem.TYPE_HOT:
                GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4);
                viewHolder.recyclerView.setLayoutManager(gridLayoutManager);
                viewHolder.recyclerView.setAdapter(mHotAdapter);
                viewHolder.recyclerView.setFocusable(false);
                mHotAdapter.setNewData(mHotSingerList);
                mHotAdapter.setOnItemClickListener((adapter, v, position) -> {
                    SingerList singerList = mHotSingerList.get(position);
                    if (mListener != null) {
                        Singer singer = new Singer();
                        singer.id = singerList.getSingerId() + "";
                        mListener.singerDidClick(singer);
                    }
                });
                break;
            case DataItem.TYPE_RECENTLY:
                GridLayoutManager glm = new GridLayoutManager(mContext, 4);
                viewHolder.recyclerView.setLayoutManager(glm);
                viewHolder.recyclerView.setAdapter(mRecentAdapter);
                viewHolder.recyclerView.setFocusable(false);
                mRecentAdapter.setNewData(mFreSingerList);

                mRecentAdapter.setOnItemClickListener((adapter, v, position) -> {
                    SingerList singerList = mFreSingerList.get(position);
                    if (mListener != null) {
                        Singer singer = new Singer();
                        singer.id = singerList.getSingerId() + "";
                        mListener.singerDidClick(singer);
                    }
                });
                break;
        }
        return view;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < dataItemList.size()) {
            DataItem item = dataItemList.get(position);
            return item.getType();
        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    public class ViewHolder {
         ImageView imageView;
         TextView nameView;
         View lineView;
         RecyclerView recyclerView;
    }
}
